package com.xvdong.gadget;


import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.*;

/**
 * Created by xvDong on 2025/8/2.
 */
public class AutoClicker {

    private static final int CLICK_X = 1000; // 你自己调整
    private static final int CLICK_Y = 600;
    private static final int INTERVAL_MS = 500; // 每次点击间隔

    private static final AtomicBoolean running = new AtomicBoolean(false);

    public static void main(String[] args) throws Exception {
        Robot robot = new Robot();

        JOptionPane.showMessageDialog(null,
                "按 F8 开始点击，按 F9 停止点击，Esc 退出程序",
                "自动点击器", JOptionPane.INFORMATION_MESSAGE);

        // 启动监听线程
        new Thread(() -> {
            try {
                while (true) {
                    if (KeyboardFocusManager.getCurrentKeyboardFocusManager()
                            .getActiveWindow() != null) {
                        if (isKeyPressed(KeyEvent.VK_F8)) {
                            running.set(true);
                            System.out.println("点击启动");
                        } else if (isKeyPressed(KeyEvent.VK_F9)) {
                            running.set(false);
                            System.out.println("点击停止");
                        } else if (isKeyPressed(KeyEvent.VK_ESCAPE)) {
                            System.out.println("退出程序");
                            System.exit(0);
                        }
                    }

                    if (running.get()) {
                        robot.mouseMove(CLICK_X, CLICK_Y);
                        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                        Thread.sleep(INTERVAL_MS);
                    } else {
                        Thread.sleep(100);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }

    // 模拟判断键是否被按下（仅演示用）
    private static boolean isKeyPressed(int keyCode) {
        return (Toolkit.getDefaultToolkit().getLockingKeyState(keyCode));
    }
}
